package com.dao.impl;

import org.hibernate.*;
import org.springframework.stereotype.Repository;

import com.dao.UserDao;
import com.entity.User;
@Repository
public class UserDaoImpl extends BaseDaoImpl implements UserDao {
	@Override
	public int save(User user) {
		this.getSession().save(user);
		return user.getId();
	}

	@Override
	public void update(User user) {
		Session session=this.getSession();
		session.merge(user);
		session.update(user);
	}

	@Override
	public User getUser(String email, String password) {
		String hql="from User where email=:email and password=:password";
		Session session=this.getSession();
		Query query=session.createQuery(hql);
		query.setString("email", email);
		query.setString("password", password);
		return (User) query.uniqueResult();
	}

	@Override
	public boolean getUser(String email) {
		String hql="from User where email=:email";
		Session session=this.getSession();
		Query query=session.createQuery(hql);
		query.setString("email", email);
		Object user=query.uniqueResult();
		if(user!=null)
			return true;
		else
			return false;
	}

	@Override
	public User getUser(int id) {
		return (User) getSession().get(User.class, id);
	}

}
